package cn.kgc.message.controller;

import cn.kgc.message.entiy.Message;
import cn.kgc.message.service.MessageService;
import cn.kgc.message.utils.IpUtil;
import cn.kgc.message.utils.ResponseResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author 课工场
 * @date 2024/1/10
 * @description
 */
@RestController
@RequestMapping("message")
public class MessageController {

    @Resource
    private MessageService messageService;

    // 保存留言
    @PostMapping("add")
    public ResponseResult  addMessage(@RequestBody Message message, HttpServletRequest request){

        //1. 获取请求ip
        String ip = IpUtil.getIp(request);
        message.setDeviceId(ip);
        return  messageService.addMessage(message);
    }


    // 暂存留言
    @PostMapping("temp")
    public ResponseResult tempMessage(@RequestBody Message message, HttpServletRequest request){

        String ip = IpUtil.getIp(request);
        message.setDeviceId(ip);
        return  messageService.tempMessage(message);
    }

    // 获取留言   暂存   提交  ip
    @GetMapping
    public ResponseResult  getMessageByDeviceId(HttpServletRequest request){

        String ip = IpUtil.getIp(request);
        return  messageService.getMessageByDeviceId(ip);
    }

}
